package actor.remote.url

import actor.remote.url.Messages._
import akka.actor.{Actor, ActorSystem, OneForOneStrategy, Props, SupervisorStrategy}
import com.typesafe.config.ConfigFactory

import scala.concurrent.duration._

/**
 * Created by DJJ on 2019/12/24.
 */

class SupervisorActor extends Actor {
  def decider: PartialFunction[Throwable,SupervisorStrategy.Directive] = {
    case _: ArithmeticException => SupervisorStrategy.Resume
  }

  override def supervisorStrategy: SupervisorStrategy =
    OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange = 5 seconds){
      decider.orElse(SupervisorStrategy.defaultDecider)
    }

  val calcActor = context.actorOf(CalcProps.props,"calculator")

  override def receive: Receive = {
    case msg@ _ => calcActor.forward(msg)
  }

}

object CalculatorRunner extends App {

  val remoteSystem = ActorSystem("remoteSystem",ConfigFactory.load("remote"))
  val calcActor = remoteSystem.actorOf(Props[SupervisorActor],"supervisorActor")
  /*
    import remoteSystem.dispatcher

    calcActor ! Clear
    calcActor ! Num(13.0)
    calcActor ! Mul(1.5)

    implicit val timeout = akka.util.Timeout(1 second)

    ((calcActor ? GetResult).mapTo[String]) foreach println
    scala.io.StdIn.readLine()

    calcActor ! Div(0.0)
    calcActor ! Div(1.5)
    calcActor ! Add(100.0)
    ((calcActor ? GetResult).mapTo[String]) foreach println
    */
  scala.io.StdIn.readLine()
  remoteSystem.terminate()

}


